MsgN("-- entities.lua loaded")

function CreateEntity( ply, pos, type, grow )

	local model = MDRP.Seeds[type][math.random(1,#MDRP.Seeds[type])]

	local seed = ents.Create("mdrp_base")
	seed:SetModel( model )
	seed:SetPos(pos)
	seed:DropToFloor()
	seed:Spawn()

	if (ply) and (ply:IsValid()) then
		if (grow) then local time = math.Clamp((10 / (ply:GetSkill( "Farming" ) + 1)), 10, 99999999) end

		seed.Type = type	--save seed type
		seed.Owner = ply	--save owner

		--add to seed spanwed count
		ply:AddItemCount(type)

		--add to undo list
		undo.Create( "Seed" .. type )
		undo.AddEntity( seed )	--add new ent to undo
		undo.SetPlayer( ply )	--save undo to player
		undo.Finish()

		--add to clean up
		ply:AddCleanup( "Seed" .. type, seed )
	end

	if (grow) then
		seed:SetNWFloat("SpawnTime", CurTime())
		seed:SetNWInt("GrowthTime", 10 )
	end
end

function createStump( pos )
	local stump = ents.Create("mdrp_base")
	stump:SetPos( pos )
	stump:SetModel( "models/props_foliage/tree_stump01.mdl" )
	stump:Spawn()
	
	return stump
end

function createLog( pos, onfire )
	--create the log fragment
	local log = ents.Create("mdrp_base")
	log:SetMoveType( MOVETYPE_VPHYSICS )
	log:SetModel( MDRP.Logs[math.random(1,#MDRP.Logs)] )
	log:SetPos( pos + Vector(math.random(1, 15), math.random(1, 15), 0) )
	log:Spawn()
	log:Activate()

	--if tree was on fire, so are the logs :)
	if (onfire) then log:Ignite() end
	
	return log
end